In-memory HttpServer Handler
Christian Stein on November 6, 2023Inspired by JEP’s 408 summary, which reads:
Summary Provide a command-line tool to start a minimal web server that serves static files in the current directory. This low-threshold utility will be useful for prototyping, ad-hoc coding, and testing purposes, particularly in educational contexts.
Here’s a http handler implementation that servers in-memory assets. This is useful for running http client tests that want to issue GET or HEAD requests against a well-known set of assets.
Asset
An asset record encapsulates a http return code, the bytes to send, and a type component that is used as
the Content-Type
response header.
All well-known assets are organized in a Map<String, Asset>
, with absolute request paths used as keys.
By default, all paths representing a directory (end with /
) are mapped to an asset that generates a directory listing.
All other unmapped paths (non-directory) are mapped to an asset that renders a 404 error page.
Command-line Demo
Launch an in-memory http server demo on the command-line via:
Open the URL in a browser: